<template>
  <div class="directorys-container">
    <el-card>
      <div style="margin-bottom: 20px" v-if="subject.id && subject.name">
        <el-breadcrumb>
          <el-breadcrumb-item>学科管理</el-breadcrumb-item>
          <el-breadcrumb-item>{{ subject.name }}</el-breadcrumb-item>
          <el-breadcrumb-item>目录管理</el-breadcrumb-item>
        </el-breadcrumb>
      </div>
      <!-- 筛选 -->
      <el-row>
        <el-col :span="18">
          <el-form label-width="70px" size="small" :inline="true">
            <el-form-item label="目录名称">
              <el-input
                style="200px"
                v-model="bottomName.directoryName"
              ></el-input>
            </el-form-item>
            <el-form-item label="状态">
              <el-select v-model="bottomName.state" placeholder="请选择">
                <el-option label="开启" :value="0"></el-option>
                <el-option label="禁用" :value="1"></el-option>
              </el-select>
            </el-form-item>
            <el-button @click="clear()">清除</el-button>
            <el-button type="primary" @click="search()">搜索</el-button>
          </el-form>
        </el-col>
        <el-col :span="6" style="text-align: right">
          <el-button
            type="info"
            icon="el-icon-back"
            v-if="subject.id && subject.name"
            @click="black"
            >返回学科</el-button
          >
          <el-button type="success" icon="el-icon-edit" @click="postAdd"
            >新增目录</el-button
          >
        </el-col>
      </el-row>
      <!-- 数据 -->
      <el-alert
        :title="`一共有${total}条数据`"
        type="info"
        center
        show-icon
        :closable="false"
      >
      </el-alert>
      <!-- 表格 -->
      <el-table :data="directorys">
        <el-table-column label="序号" type="index"></el-table-column>
        <el-table-column label="所属学科" prop="subjectName"></el-table-column>
        <el-table-column
          label="目录名称"
          prop="directoryName"
        ></el-table-column>
        <el-table-column label="创建者" prop="username"></el-table-column>
        <el-table-column label="创建日期">
          <template slot-scope="scope">{{
            scope.row.addDate | parseTimeByString
          }}</template>
        </el-table-column>
        <el-table-column label="面试题数量" prop="totals"></el-table-column>
        <el-table-column label="状态">
          <template slot-scope="scope">{{
            scope.row.state === 0 ? "已开启" : "已禁用"
          }}</template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="text" @click="postChangeState(scope.row)">{{
              scope.row.state === 0 ? "禁用" : "开启"
            }}</el-button>
            <el-button
              type="text"
              @click="postUpdate(scope.row)"
              :disabled="scope.row.state === 0"
              >修改</el-button
            >
            <el-button
              type="text"
              :disabled="scope.row.state === 0"
              @click="putRemove(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="bottomName.page"
        :page-sizes="[3, 4, 5, 10]"
        :page-size="bottomName.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        style="margin-top: 20px; text-align: center"
      >
      </el-pagination>
      <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="40%">
        <!-- :before-close="handleClose" -->
        <el-form
          :rules="rules"
          :model="formModel"
          ref="changeForm"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="所属学科">
            <el-select
              v-model="formModel.subjectID"
              placeholder="请选择所属学科"
            >
              <el-option
                v-for="item in simpleSubject"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="目录名称" prop="directoryName">
            <el-input v-model="formModel.directoryName"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="closeDialog">取 消</el-button>
          <el-button type="primary" @click="saveForm">确 定</el-button>
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import {
  list,
  remove,
  changeState,
  update,
  add,
} from "../../api/hmmm/directorys";
import DirectorysAdd from "../components/directorys-add.vue";
import { simple } from "@/api/hmmm/subjects";
export default {
  name: "DirectorysAdd",
  components: { DirectorysAdd },
  data() {
    return {
      total: 0,
      isAdd: false,
      firstHalf: {
        directoryName: null,
        state: null,
      },
      // modifyDirectory
      formModel: {
        id: null,
        subjectID: null, // 所属学科
        directoryName: null, //目录名称
      },
      directorys: [],
      //所有的所需学科
      simpleSubject: [],
      dialogVisible: false,
      rules: {
        //验证表单的规则
        directoryName: [
          { required: true, message: "请输入活动名称", trigger: "blur" },
        ],
      },
      dialogTitle: "请输入内容",
      bottomName: {
        page: 1,
        pagesize: 10,
        directoryName: null,
        state: null,
      },
    };
  },
  methods: {
    async getList() {
      const { data } = await list(this.bottomName);
      this.directorys = data.items;
      this.total = data.counts;
      console.log(data);
      // console.log(this.directorys, "表格数据源");
    },
    async putRemove(data) {
      await this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          remove(data);
          this.getList();
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    async postChangeState(data) {
      let StateData = data;
      StateData.state === 0 ? (StateData.state = 1) : (StateData.state = 0);
      // console.log(StateData);
      await changeState(StateData);
      this.getList();
      this.$message.success("操作成功");
    },
    async postUpdate(data) {
      //1打开弹框
      this.dialogTitle = "修改目录";
      this.dialogVisible = true;
      const { data: result } = await simple(data); //结构赋值获取data命名result
      this.simpleSubject = result;
      this.formModel.directoryName = data.directoryName;
      this.formModel.id = data.id;
      console.log(this.simpleSubject, "数组");
    },
    // 保存修改
    //判断是否为新增
    async saveForm() {
      if (!this.isAdd) {
        console.log("修改");
        this.$refs.changeForm.validate(async (valid) => {
          this.isAdd = false;
          if (valid) {
            try {
              await update(this.formModel);
              await this.getList();
            } catch (err) {
              this.$message.error(err);
            }
            this.closeDialog();
          } else {
            // alert(1);
            return false;
          }
        });
      } else {
        console.log("新增");
        this.$refs.changeForm.validate(async (valid) => {
          if (valid) {
            try {
              console.log(this.formModel);
              await add(this.formModel);
              await this.getList();
            } catch (err) {
              this.$message.error(err);
            }
          } else {
            // alert(1);
            return false;
          }
          this.closeDialog();
        });
        this.isAdd = false;
      }

      console.log(this.$refs.changeForm, "标高为");
      // console.log(this.formModel, "yghjg ");
    },
    //关闭弹窗执行的方法
    closeDialog() {
      this.formModel.id = null; // 清空表单的数据
      this.formModel.subjectID = null; // 清空表单的数据
      this.formModel.directoryName = null; // 清空表单的数据
      this.$refs.changeForm.resetFields(); //重置表单
      this.dialogVisible = false;
      // console.log(this.formModel, "看");
    },
    async postAdd() {
      this.dialogTitle = "新增目录";
      this.dialogVisible = true;
      this.isAdd = true;
      const { data: result } = await simple(); //结构赋值获取data命名result
      this.simpleSubject = result;
      // console.log(this.formModel, "111ss");
    },
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
      this.bottomName.pagesize = val;
      this.getList();
      console.log(this.bottomName.pagesize, "pagesize");
    },
    handleCurrentChange(val) {
      this.bottomName.page = val;
      this.getList();
      console.log(this.bottomName.page, "page");
      // console.log(`当前页: ${val}`);
    },
    // 清楚筛选
    clear() {
      this.bottomName = {
        directoryName: null,
        state: null,
      };
    },
    //搜索按钮
    search() {
      this.bottomName.page = 1;
      this.getList();
    },
    black() {
      this.$router.go(-1);
    },
  },

  // async simple(){
  // },
  // 清除筛选条件
  computed: {
    subject() {
      return this.$route.query || {};
    },
  },
  created() {
    this.getList();
  },
  watch: {
    "$route.query": function () {
      this.getList();
    },
    "bottomName.state": (newData, oldData) => {
      console.log("切换了选项", newData, oldData);
    },
  },
};
</script>

<style lang="scss" scoped >
.directorys-container {
  padding: 10px;
}
</style>
